import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

//导入组件
import Movie from '@/views/Movie/index.vue'
import Cinema from '@/views/Cinema/index.vue'
import My from '@/views/My/index.vue'
import News from '@/views/News/index.vue'
import nowplaying from '../views/Movie/nowplaying.vue'
import moviedetail from '@/views/Movie/moviedetail.vue'
import store from '@/store'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/movie'
  },
  {
    path: '/movie',
    component: Movie,
    children: [
      {
        path: 'moviedetail',
        component: moviedetail
      }
    ]
  },
  {
    path: '/cinema',
    component: () => import('@/views/Cinema/cinema.vue'),
    redirect: '/cinema/index',
    children: [
      {
        path: 'index',
        component: Cinema
      },
      {
        path: 'film',
        component: () => import('@/views/Cinema/film.vue')
      }
    ]
  },
  {
    path: '/my',
    component: My,
    redirect: '/my/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/My/Myindex.vue')
      },
      {
        path: 'login',
        component: () => import('@/views/My/login.vue')
      },
      {
        path: 'register',
        component: () => import('@/views/My/register.vue')
      }
    ]
  },
  {
    path: '/news',
    component: News
  },
  {
    path: '/city',
    component: () => import('@/views/City/index.vue')
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(to, from);
  let arr = ['/movie', '/cinema/index', '/news', '/my/index']
  let index = arr.indexOf(to.path)
  // console.log(arr, to.path, index);
  store.commit('changeActive', index)
  next()
})

export default router
